<template>
  <div class="app-container">
    <!-- zwz-选择专家对话框 -->
    <el-dialog v-model="pageConfig.expertOpen" :title="pageConfig.title" width="850px" @closed="handleClose" append-to-body>
      <el-form :model="pageConfig.queryParams" ref="queryForm" :inline="true" v-show="pageConfig.showSearch" label-width="68px">
        <el-form-item label="专家姓名" prop="realName">
          <el-input v-model.trim="pageConfig.queryParams.realName" placeholder="请输入专家姓名" clearable @keyup.enter.native="handleQuery" style="width: 200px" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model.trim="pageConfig.queryParams.unit" placeholder="请输入单位" clearable @keyup.enter.native="handleQuery" style="width: 200px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table ref="multipleTableRef" v-loading="pageConfig.loading" :data="pageConfig.dataList" border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="95" label="序号" align="center" />
        <el-table-column label="姓名" align="center" prop="realName" />
        <el-table-column label="性别" align="center" prop="sex" width="80">
          <template #default="scope">
            <dict-tag :options="sys_user_sex" :value="scope.row.sex" />
          </template>
        </el-table-column>
        <el-table-column label="手机号" align="center" prop="userMobile" width="130">
          <template #default="scope">
            {{ isItAnEmptyField(scope.row.userMobile) }}
          </template>
        </el-table-column>
        <el-table-column label="单位" align="center" prop="unit" width="230" />
        <el-table-column label="职称" align="center" prop="expertTitle" width="130">
          <template #default="scope">
            {{ isItAnEmptyField(scope.row.expertTitle) }}
          </template>
        </el-table-column>
        <el-table-column label="证书有效期" align="center" prop="certificateValidity" width="230">
          <template #default="scope">
            <span>{{ scope.row.validityStartDate }} - {{ scope.row.validityEndDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="出生年月" align="center" prop="birthday" />
      </el-table>
      <!-- 分页组件 -->
      <pagination v-show="pageConfig.total > 0" :total="pageConfig.total" v-model:page="pageConfig.queryParams.pageNum" v-model:limit="pageConfig.queryParams.pageSize" @pagination="getList" />

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="handleClose">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="expertInfo">
import { listExpertInfo } from "@/api/expertInfo/expertInfo";
const { proxy } = getCurrentInstance();
const { sys_user_sex } = proxy.useDict("sys_user_sex");

const props = defineProps({
  open: {
    type: Boolean,
    default: false,
  },
  list: {
    type: Array,
    default: () => [],
  },
});
import { reactive, ref, nextTick } from "vue";
const selectList = computed(() => props.list);
const pageConfig = reactive({
  // 遮罩层
  loading: false,
  // 表单校验
  rules: {},
  // 弹出框
  expertOpen: false,
  // 显示搜索条件
  showSearch: true,
  // 数据项
  dataList: [],
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    realName: null,
    unit: null,
  },
  // 对话框标题
  title: "选择专家",
});

// 监听选择专家弹窗开关
watch(
  () => props.open,
  (value) => {
    pageConfig.expertOpen = value;
    if (pageConfig.expertOpen) {
      getList();
    }
  }
);

/** 查询专家信息列表 */
function getList() {
  pageConfig.loading = true;
  listExpertInfo(pageConfig.queryParams)
    .then((response) => {
      pageConfig.dataList = response.rows;
      pageConfig.total = response.total;
    })
    .finally(() => {
      pageConfig.loading = false;
    });
}

/** 搜索按钮操作 */
function handleQuery() {
  pageConfig.queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}

// 选择专家的数据
const multipleSelection = ref([]);

// 清空之前选中的
multipleSelection.value = [];

const emit = defineEmits(["update", "open"]);

// 选择专家
function handleSelectionChange(val) {
  multipleSelection.value = val;
}

// 确定选择专家
function submitForm() {
  if (!multipleSelection.value.length) {
    proxy.$modal.msgWarning("请选择专家！");
    return;
  }
  if (multipleSelection.value.length > 5) {
    proxy.$modal.msgWarning("专家最多指定 5 位");
    return;
  }

  let zongList = [...selectList.value, ...multipleSelection.value];
  const uniqueListByUserId = zongList.reduce((accumulator, current) => {
    if (!accumulator.find((item) => item.userId === current.userId)) {
      accumulator.push(current);
    }
    return accumulator;
  }, []);

  if (uniqueListByUserId.length > 5) {
    proxy.$modal.msgWarning("专家最多指定 5 位");
    return;
  }
  emit("selectExpert", uniqueListByUserId);
  emit("update:open", false);
}
// table ref
const multipleTableRef = ref();
// 取消 关闭弹窗
function handleClose() {
  multipleSelection.value = [];
  emit("update:open", false);
  multipleTableRef.value.clearSelection();
}
</script>
<style scoped lang="scss"></style>
